
var em_hdrinmgr;

CFealtyMgr = function() {
	
	this.objName = 'CFealtyMgr: ';
	
	this.reset();


	// this.hdrin = new CHDrin();
	var self = this;
	em.cbs.userchanged.add(function() {self.reset();});
	
		// add global event cb 
	em.cbs.forcestop.add ( function() { self.force_stop = true; });

};



CFealtyMgr.prototype = {

	reset : function() {

		this.started = false;
		this.lasttime = new Date(parseInt(localStorage.getItem('Fealty_lasttime_' +  CDB.getCurUserName()) ) );
		if ( ! this.lasttime ) {
			this.lasttime = new Date();
		}
	 },

	// move cstl.. in turn , h drin
	startWave: function() {
		var dfd = this.wholedone =  $.Deferred();
		
		// info 
		em.util.print_result(' . last try was : ' + this.lasttime.getHours() + ';'+ this.lasttime.getMinutes() );		
	
		this.doHFealty();
		
		return dfd.promise();
	},
	
	
	// func after move cstl 
	afterMove: function(data) {
		// start 
		// this.doConscribe( em.cstlmgr.curCstlID());		
		
		var self = this;
		setTimeout( function() { self.doConscribe( em.cstlmgr.curCstlID()); } ,CUtil.RandTime(2000));			
	},
	
	// entry func 
	doHFealty: function () {

		debug.log(this, retName(this) , ' go Fealty !!! ');

		// register  cstl move cb 
		var self = this;

		// reset inital value 
		self.force_stop = false;

		if ( this.hfealty_cb) delete this.hfealty_cb;
		this.hfealty_cb = $.Callbacks();
		
		this.hfealty_cb.add( function (data) {
			
			// check force stop 
			if ( this.force_stop ) {
				debug.log ( this, ' Froced Stopped !!! ');
				return ;
			}
			debug.log(self, 1, data);
			if(data.ret.fealty) {
				// update fealty value to hro
				 em.cstlmgr.updateHF(self.h, data.ret.fealty);
				 self.doCurH();
			} else {
				// error !! : move next H 
				self.doNextH();
			}
			 
		});
		
		// go start with first ... <- after function : will start h drin 
		// em.cbs.cstlmove1done.add( function() { self.afterMove();});
		em.cbs.cstlmove1done.add( this.cb_cstlmove = function( ){ self.afterMove();}  );
		em.cbs.cstlwavedone.add( this.cb_wavedone = function() { self.completed();});	
		// em.cbs.cstlwavedone.add( function() { self.completed();});	
		em.cstlmgr.moveCstlAt(0);

	},

	// all done 
	completed: function(data) {
		console.log('cb called complted!!!!!!!');
		this.started = true;
		this.lasttime = new Date();
		
		localStorage.setItem('Fealty_lasttime_' +  CDB.getCurUserName() , this.lasttime.getTime());
		
		// remove cbs 
		em.cbs.cstlmove1done.remove(this.cb_cstlmove);
		em.cbs.cstlwavedone.remove(this.cb_wavedone);
		
		this.wholedone.resolve();
		
	},



	doCurH: function() {
		var self = this;
		setTimeout( function() { em.tasks.doFealty(self.h, self.hfealty_cb); } ,CUtil.RandTime(500));	
	},

	doNextH: function() {
		//pop hero
		var h = this.h = this.heroes.pop();
		if (! h) {
			// all done 
			debug_log( "no  H in here alll done  : move next   ");
			//  go next cstl 
			em.cstlmgr.moveNextCstl();
			return;
		}
		
		// do with  tasks
		// em.tasks.doFealty(h, this.hfealty_cb);
		var self = this;
		setTimeout( function() { em.tasks.doFealty(h, self.hfealty_cb); } ,CUtil.RandTime(500));	

	},
	
	
	doConscribe: function( citnum) {
		
		var self = this;
		/*
		{"id":9176,"gid":210,"p":44,"i":8,"c1":17,"f":34,"g":12,"c2":613,"fy":0,"s":0,"e":10,"w":0,"tw":30,"tl":79,
	"ex":59758,"te":257100,"np":0,"ni":0,"nc1":0,"nc2":0,"ns":0,"ncd":0,"pr":36000},
	*/
		function checkConscribe (heroes) {
			// data = hero 
			if ( heroes.length == 0) {
				// no Hero!! 
				debug_log( "no HHHHHero in here  ");
				//  go next cstl 
				em.cstlmgr.moveNextCstl();
				return;
			} else {
				
				// hro list 구성.  
				// recv data is array . 
				em.cbs.heroupdate.fire(heroes)
				
				this.heroes = heroes;
				// go next action :::   
				self.doNextH();
			}
		};	
		
		//&city=14281&action=gen_list&extra=1
		var param = { citnum: citnum,  cb : function (data) { 
				checkConscribe.call(self, data); }
		};
		em.ahelper.conscribe(param);
	},

};

 

 
